import { useContentStore } from '@/stores/content'
import { storeToRefs } from 'pinia'
import { showToast } from 'vant/es'
import { createRouter, createWebHistory, onBeforeRouteUpdate } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'index',
      meta: { keepAlive: true },
      redirect: '/films',
      component: () => import('@/views/index.vue'),
      children: [
        {
          path: 'films', //首页
          name: 'films',
          meta: { keepAlive: true },
          component: () => import('@/views/index/films/index.vue')
        },
        {
          path: 'category', //分类
          name: 'category',
          meta: { keepAlive: true },
          component: () => import('@/views/index/category/index.vue')
        },
        {
          path: 'cart', //购物车
          name: 'cart',
          meta: { keepAlive: true },
          component: () => import('@/views/index/cart.vue')
        },
        {
          path: 'content', //我的
          name: 'content',
          meta: { keepAlive: true },
          component: () => import('@/views/index/content/index.vue')
        }
      ]
    },

    {
      path: '/order', // 订单页面
      name: 'order',
      component: () => import('@/views/order.vue')
    },
    {
      path: '/login', // 登录页面
      name: 'login',
      component: () => import('@/views/login/index.vue')
    },
    {
      path: '/browsingHistory', // 浏览记录
      name: 'browsingHistory',
      meta: { keepAlive: true },
      component: () => import('@/views/browsingHistory.vue')
    },
    {
      path: '/supermarket', // 京东超市
      name: 'supermarket',
      meta: { keepAlive: true },
      component: () => import('@/views/supermarket.vue')
    },
    {
      path: '/electricalAppliances', // 京东电器
      name: 'electricalAppliances',
      meta: { keepAlive: true },
      component: () => import('@/views/electricalAppliances.vue')
    },
    {
      path: '/search', // 京东首页搜索
      name: 'search',
      meta: { keepAlive: true },
      component: () => import('@/views/search/index.vue')
    },
    {
      path: '/address', // 地区选择
      name: 'address',
      component: () => import('@/views/address.vue')
    },
    {
      path: '/account', //账户设置
      name: 'account',
      meta: { keepAlive: true },
      component: () => import('@/views/account.vue')
    },
    {
      path: '/detail/:id?', //商品详情
      name: 'detail',
      meta: { keepAlive: true },
      component: () => import('@/views/detail/index.vue')
    },
    {
      path: '/orders/:id?', //订单确认
      name: 'orders',
      meta: { keepAlive: true },
      component: () => import('@/views/orders/index.vue')
    }
  ]
})

//路由守卫
router.beforeEach((to) => {
  const { username } = storeToRefs(useContentStore())
  if (to.name === 'content' && !username.value) {
    //重定向到登录

    showToast('请登录后查看个人中心')
    return {
      name: 'login',
      query: { redirect: to.fullPath }
    }
  }
  if (to.name === 'detail' && !username.value) {
    //重定向到登录

    showToast('请登录后查看商品')
    return {
      name: 'login',
      query: { redirect: to.fullPath }
    }
  }
  if (to.name === 'address' && !username.value) {
    //重定向到登录

    showToast('非法访问')
    return {
      name: 'login',
    }
  }

  return true
})

export default router
